package com.oc.claimobs.bb.commands;

import java.util.Hashtable;

import javax.microedition.location.Location;

import net.rim.device.api.servicebook.ServiceBook;
import net.rim.device.api.servicebook.ServiceRecord;
import net.rim.device.api.ui.UiApplication;
import ar.com.lemil.bb.http.commands.CommandCallback;
import ar.com.lemil.bb.http.commands.HttpService;
import ar.com.lemil.bb.http.detection.BlackberryConnections;
import ar.com.lemil.bb.http.vo.HttpRequest;

import com.oc.claimobs.bb.ScreenFlow;
import com.oc.claimobs.bb.facade.InfoService;
import com.oc.claimobs.bb.facade.ReclamoService;
import com.oc.claimobs.bb.model.Info;
import com.oc.claimobs.bb.model.Reclamo;
import com.oc.claimobs.bb.screens.SendingScreen;


public class SendReclamoCommand implements Runnable {

	SendingScreen screen = null;
	String slog;

	public SendingScreen getScreen() {
		return screen;
	}
	public void setSendingScreen(SendingScreen screen) {
		this.screen = screen;
	}
	
	public void run() {
		
		boolean success = false;
		
		try {
			//
			String url = "http://cippec.ath.cx/reclamos/services/xml/reclamos.php";
			//
			HttpRequest payload = getAttached();
			HttpRequest[] attachments = new HttpRequest[]{payload};
			//
			Hashtable parameters = getParameters();
			//
			HttpService service = new HttpService();
			service.setScreen(screen);
			service.setAttachments(attachments);
			service.setParameters(parameters);
			service.setUrl(url);
			//service.setUrl("http://lemil.com.ar/conntest/test.php");
			//
			Hashtable connections = service.getAvailableConnections();
			
			if(parameters.containsKey("carrier")){
				String carrier = (String)parameters.get("carrier");
				String carrierconn = (String)BlackberryConnections.HTTP_WAP_CARRIERS_SETTINGS.get(carrier);
				connections.put(BlackberryConnections.CARRIER_CONN, carrierconn );
			}
			//
			success = service.executePlan(connections);
			if(success){
				screen.status("Enviado");
				Thread.sleep(1500);
			}
		} catch(Exception ex){
			success = false;
		}
		
		if(success){
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					screen.messageSent();
				}
			});
		} else {
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					screen.reintentar(false);
				}
			});
		}
	}
	
	public HttpRequest getAttached(){
		//
		byte[] fileBytes = ScreenFlow.getInstance().getLastPicture();
		//
		HttpRequest payload = new HttpRequest( "reclamo.jpg", "image/jpg", "picture", fileBytes);
		return payload;
	}
	
	public Hashtable getParameters(){
		
		InfoService service = new InfoService();
		Integer k = new Integer(1);
		Info o = service.getByKey(k);
		if(o == null){
			throw new RuntimeException("Aun no se ha cargado la informacion personal");
		}
		//
		ReclamoService rservice = new ReclamoService();
		int sessid = ScreenFlow.getInstance().getCurrentSession();
		Integer rk = new Integer(sessid);
		Reclamo r = rservice.getByKey(rk);
		if (r == null) {
			throw new RuntimeException("No se encontro la informacion del reclamo");
		}
		
		//
		Location loc = ScreenFlow.getInstance().getCurrentLocation();
		String lat = "0.00000";
		String lon = "0.00000";
		if(loc != null && loc.getQualifiedCoordinates() != null){
			lat = String.valueOf(loc.getQualifiedCoordinates().getLatitude());
			lon = String.valueOf(loc.getQualifiedCoordinates().getLongitude());
		}
		//
		Hashtable params = new Hashtable();
		params.put("nombre", o.getNombre());
		params.put("apellido", o.getApellido());
		
		String valTipoDoc = o.getTipodoc();
		int inicio = valTipoDoc.indexOf("|");
		String val = valTipoDoc.substring(inicio + 1);
		params.put("doc_tipo", val);
		params.put("doc", o.getDocumento());
		params.put("telefono", o.getTelefono());
		
		String tipoR = r.getReclamoTipo();
		int inicioTipoR = tipoR.indexOf("|");
		String valTipoR = tipoR.substring(inicioTipoR + 1);		
		params.put("tipo", valTipoR);
		
		String calleR = r.getCalle();
		int inicioCalleR = calleR.indexOf("|");
		String valCalleR = calleR.substring(inicioCalleR + 1);
		params.put("calle", valCalleR);
		
		params.put("calle_alt", r.getAlturaCalle());
		params.put("gps_lat", lat);
		params.put("gps_long", lon);
		params.put("carrier", o.getCarrier());
		params.put("email", o.getEmail());
		
		return params;

	}


}
	

